from business.UnpackedData import UnpackedData 
import logging

class MftFile:
    def __init__(self):
        self.mHeaderDataDef = UnpackedData("MftFileHeader",
                                           [["id"         , "4s"],
                                            ["unknown1"   , "Q"] ,
                                            ["nbOfEntries", "I"] ,
                                            ["unknown2"   , "I"] ,
                                            ["crc"        , "I"]])
        self.mEntryDataDef = UnpackedData("MftEntry",
                                          [["fileOffset" , "Q"] ,
                                           ["fileSize"   , "I"],
                                           ["compression", "H"],
                                           ["content"    , "B"],
                                           ["contentType", "B"],
                                           ["unknown"    , "I"],
                                           ["crc"        , "I"]])
        self.mOffset = None
        self.mSize = None
        self.mHeader = None
        self.mEntries = list()
    
    def parse(self, iFile, iOffset, iSize):
        logging.info("MftFile: Offset(" + str(iOffset) + ") Size(" + str(iSize) + ")")
        self.mOffset = iOffset
        self.mSize = iSize
        
        iFile.seek(self.mOffset)
        lInput = iFile.read(self.mSize)
        lCurrentPos = 0
        
        self.mHeader, lCurrentPos = self.mHeaderDataDef.unpackWithOffset(lInput, lCurrentPos)
        logging.debug(self.mHeader)
        for lIndex in range(self.mHeader.nbOfEntries - 1):
            lEntry, lCurrentPos = self.mEntryDataDef.unpackWithOffset(lInput, lCurrentPos)
            logging.debug(lEntry)
            self.mEntries.append(lEntry)
    
    def getFile(self, iEntryNumber): # -1 since we are in an array
        return self.mEntries[iEntryNumber - 1]
